function ajax(obj) {
  let str = "";
  for (let i in obj.data) {
    str += i + "=" + obj.data[i] + "&";
  }
  str = str.replace(/&$/, ""); //把结尾的&去掉

  let xhr = new XMLHttpRequest();
  if (obj.type.toLowerCase() == "get") {
    if (obj.data) {
      xhr.open("get", obj.url + "?" + str);
    } else {
      xhr.open("get", obj.url);
    }

    xhr.send();
  }
  if (obj.type.toLowerCase() == "post") {
    xhr.open("post", obj.url);
    xhr.setRequestHeader(
      "Content-type",
      "application/x-www-form-urlencoded"
    );
    xhr.send(str);
  }
  xhr.onreadystatechange = function () {
    if (xhr.readyState == 4) {
      if (xhr.status == 200) {
        let data = xhr.responseText;
        obj.success(data);
      } else {
        obj.error();
      }
    }
  };
}